using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OpenComposite.Base.Collections;

namespace OpenComposite.EII.Forms
{
	public partial class FormSelectRepositoryItems : Form
	{
		#region Initialization
		public FormSelectRepositoryItems()
		{
			InitializeComponent();

			init();
		}
		private void init()
		{
			_bMultiSelect = true;
			_lastCheckedCell = null;

			//_lstItems = new List<OpenComposite.EII.Repository.RepositoryItemBase>();
			_lstSelectedItems = new List<OpenComposite.EII.Repository.RepositoryItemBase>();

			dgvItems.AutoGenerateColumns = false;
		}
		#endregion

		#region Public Members

		#region Properties

		public bool MultiSelect
		{
			get { return _bMultiSelect; }
			set
			{
				if ( _bMultiSelect != value ) {
					_bMultiSelect = value;
					OnMultiSelectChanged();
				}
			}
		}
		public List<Repository.RepositoryItemBase> SelectedItems
		{
			get { return _lstSelectedItems; }
		}
		//		public List<Repository.RepositoryItemBase> Items
		//{
		//    get { return _lstItems; }
		//}

		#endregion

		#region Events
		#endregion

		#region Methods
		public void SetItems(BindingListEx<Repository.RepositoryItemBase> items)
		{
			items.Sort("Name", ListSortDirection.Ascending);
			dgvItems.DataSource = items;
		}
		public void SetItems(BindingListEx<Repository.RepositoryItemBase> items, List<Repository.RepositoryItemBase> selecteditems)
		{
			items.Sort("Name", ListSortDirection.Ascending);
			dgvItems.DataSource = items;
			_lstSelectedItems.AddRange(selecteditems);
		}
		#endregion

		#endregion

		#region Protected Members

		#region Methods

		private void OnMultiSelectChanged()
		{
		}

		#endregion

		#endregion

		#region Private Members

		#region Variables

		private bool _bMultiSelect;
		private DataGridViewCheckBoxCell _lastCheckedCell;

		//		private List<Repository.RepositoryItemBase> _lstItems;
		private List<Repository.RepositoryItemBase> _lstSelectedItems;

		#endregion

		#region Methods
		#endregion

		#region Event Handlers
		private void dgvItems_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			if ( e.RowIndex < 0 )
				return;
			if ( e.ColumnIndex == colSelect.Index ) {
				DataGridViewRow row = dgvItems.Rows[e.RowIndex];
				DataGridViewCheckBoxCell cell = row.Cells[e.ColumnIndex] as DataGridViewCheckBoxCell;
				if ( cell != null ) {
					if ( _lstSelectedItems.Contains((Repository.RepositoryItemBase)row.DataBoundItem) ) {
						_lstSelectedItems.Remove((Repository.RepositoryItemBase)row.DataBoundItem);
						cell.Value = false;
						if ( _lastCheckedCell == cell ) {
							_lastCheckedCell = null;
						}
					} else {
						cell.Value = true;
						if ( !this.MultiSelect ) {
							_lstSelectedItems.Clear();
							if ( _lastCheckedCell != null && !this.MultiSelect ) {
								_lastCheckedCell.Value = false;
							}
							_lastCheckedCell = cell;
						}
						_lstSelectedItems.Add((Repository.RepositoryItemBase)row.DataBoundItem);
					}
				}
			}
		}

		private void tsbtnOK_Click(object sender, EventArgs e)
		{
			if ( this.SelectedItems.Count == 0 ) {
				MessageBox.Show("Please select an item.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
				return;
			}
			this.DialogResult = DialogResult.OK;
			this.Close();
		}
		private void tsbtnCancel_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
			this.Close();
		}

		#endregion

		private void FormSelectRepositoryItems_Load(object sender, EventArgs e)
		{
			foreach ( DataGridViewRow row in dgvItems.Rows ) {
				Repository.RepositoryItemBase item = row.DataBoundItem as Repository.RepositoryItemBase;
				if ( item != null && _lstSelectedItems.Contains(item) ) {
					row.Cells[colSelect.Index].Value = true;
				}
			}
		}

		private void dgvItems_KeyUp(object sender, KeyEventArgs e)
		{
			if ( e.Alt && e.Control && e.KeyCode == Keys.I ) {
				Repository.RepositoryItemBase item = dgvItems.SelectedRows[0].DataBoundItem as Repository.RepositoryItemBase;
				Helpers.OpenDebugViewer(item, this);
			}
		}

		#endregion

	}
}